﻿using System.Collections.Generic;
using System.Threading.Tasks;
using SpotOffer.AppConstants;
using SpotOffer.CustomViews;
using SpotOffer.DataModels;
using SpotOffer.Resource;
using SpotOffer.ViewModels;
using Xamarin;
using Xamarin.Forms;
using XLabs.Forms.Mvvm;

namespace SpotOffer.View
{
    public class Root : MasterDetailPage
    {
        public MenuPage _menu;

        public Root()
        {
            _menu = (ViewFactory.CreatePage<MenuViewModel, Page>() as MenuPage);
            _menu.ListViewMenu.ItemSelected += (sender, e) =>
            {
                if (e.SelectedItem != null)
                {
                    NavigateTo(e.SelectedItem as Category);
                    ((ListView)sender).SelectedItem = null; // de-select the row
                }
            };
            _menu.ButtonSettings.Clicked += (s, e) =>
            {
                IsPresented = false;
               /* Task.Delay(200).ContinueWith((ss) =>
                {*/
                    Device.BeginInvokeOnMainThread((() =>
                    {

                        ((NavigationPage)App.AppInstance.MasterPage.Detail).PushAsync(
                            ViewFactory.CreatePage<SettingsViewModel, SettingsPage>((v, p) =>
                            {
                            })
                                as Page, true);
                    }));

               // });  
            };
       
            _menu.ButtonCity.Clicked += (s, e) =>
            {
                IsPresented = false;
                Task.Delay(200).ContinueWith((ss) =>
                {
                    Device.BeginInvokeOnMainThread((() =>
                    {

                        ((NavigationPage)App.AppInstance.MasterPage.Detail).PushAsync(
                            ViewFactory.CreatePage<ChooseCityViewModel, ChooseCityPage>((v, p) =>
                            {
                            })
                                as Page, true);
                    }));

                }); 
            };

            Master = _menu;

            Detail = new NavigationPage(new AppViews.FrontPage())
            {
                BarBackgroundColor = AppColors.Darkblu,
                BarTextColor = AppColors.White,
                Title = "RootNav"
            };
            if (App.Cache.Get<City>(CacheKeys.City) != null)
            {
                IsPresented = true;
            }
        }

        private void NavigateTo(Category menu)
        {
            if (menu != null)
            {

                Insights.Track("MenuClicked", new Dictionary<string, string> { { "MenuClicked", "city" }, });
                Page displayPage = new DealListPage { BindingContext = new DealListViewModel(menu) };
                displayPage.Title = App.Cache.Get<City>(CacheKeys.City).name;
                Detail = new NavigationPage(displayPage)
                {
                    BarBackgroundColor = AppColors.Darkblu,
                    BarTextColor = AppColors.White,
                };
                Task.Delay(1000).ContinueWith((s) =>
                {
                    Device.BeginInvokeOnMainThread((() =>
                    {
                        IsPresented = false;
                    }));

                });
            }
        }


    }
}
